(ns picture_gallery.routes.gallery
  (:require [compojure.core :refer :all]
            [hiccup.element :refer :all]
            [picture-gallery.views.layout :as layout]
            [picture_gallery.models.db :as db]
            [picture_gallery.util
             :refer [thumb-prefix image-uri thumb-uri]]
            [noir.session :as session]
            [picture-gallery.views.layout :as layout]))

(defn thumbnail-link [{:keys [userid name]}]                ;生成包含缩略图链接的div
  (println (str userid "+"name))
  [:div.thumbnail
   [:a {:href (image-uri userid name)}
    (image (thumb-uri userid name))
           userid "'s gallery"]]
)
(defn display-gallery [userid]                              ;显示用户所有缩略图
  (or
    (not-empty (map thumbnail-link (db/images-by-user userid)))
    [:p "The user " userid " does not have any galleries"]))

(defn gallery-link [{:keys [userid name]}]
  [:div.thumbnail
   [:a {:href (str "/gallery/" userid)}
    (image (thumb-uri userid name))
    userid "'s gallery"]])

(defn show-galleries []
  (map gallery-link (db/get-gallery-previews)))


(defroutes gallery-routes
           (GET "/gallery/:userid" [userid] (layout/common (display-gallery userid))))
